package com.web.response;

import java.io.Serializable;
import org.springframework.http.HttpStatus;

/**
 * 响应结果展示
 * @author yangguanzan
 * @date 9/24/21
 * @description
 */
public class Result implements Serializable {

    /**
     * 信息提示
     */
    private String msg;
    /**
     * 状态码，对应http的statusCode，404，500，200
     */
    private Integer code;
    /**
     * 响应的数据
     */
    private Object data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Result(String msg, Integer code, Object data) {
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public Result(String msg, Integer code) {
        this.msg = msg;
        this.code = code;
    }

    /**
     * 错误的请求数据
     * @param msg 错误提示
     * @return Result
     */
    public static Result badRequest(String msg){
        return new Result(msg, HttpStatus.BAD_REQUEST.value());
    }


    /**
     * 禁止的请求
     * @param msg 错误提示
     * @return Result
     */
    public static Result forbidded(String msg){
        return new Result(msg, HttpStatus.FORBIDDEN.value());
    }
    /**
     * 响应成功
     * @param msg 提示
     * @return Result
     */
    public static Result success(String msg){
        return new Result(msg, HttpStatus.OK.value());
    }
    /**
     * 响应成功
     * @return Result
     */
    public static Result success(){
        return new Result(HttpStatus.OK.name(), HttpStatus.OK.value());
    }
    /**
     * 响应成功
     * @param object 响应数据
     * @return Result
     */
    public static Result success(Object object){
        return new Result(HttpStatus.OK.name(), HttpStatus.OK.value(),object);
    }
    /**
     * 响应失败
     * @param msg 提示
     * @return Result
     */
    public static Result fail(String msg){
        return new Result(msg, HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    /**
     * 判断结果是否成功
     * @return 是否成功
     */
    public boolean isSuccess(){
        if(null==this.code){
            return false;
        }
        return HttpStatus.OK.value()==this.code.intValue();
    }
    @Override
    public String toString() {
        return "Result{" +
            "msg='" + msg + '\'' +
            ", code='" + code + '\'' +
            ", data=" + data +
            '}';
    }
}
